package com.hksj.Intelligentmanagement.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hksj.Intelligentmanagement.common.Rest;
import com.hksj.Intelligentmanagement.dto.NurseLevelDTO;
import com.hksj.Intelligentmanagement.dto.NurseLevelWithNurseDTO;
import com.hksj.Intelligentmanagement.entity.NurseLevelEntity;
import com.hksj.Intelligentmanagement.mapper.NurseLevelMapper;
import com.hksj.Intelligentmanagement.vo.NurseLevelVo;
import com.hksj.Intelligentmanagement.vo.NurseLevelWithNurseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 护士等级表 服务实现类
 * @author chenxingqing
 */
@Service
public class NurseLevelService extends ServiceImpl<NurseLevelMapper, NurseLevelEntity>{

    public Rest create(NurseLevelDTO nurseLevelDTO) {
        LambdaQueryWrapper<NurseLevelEntity> nurseLevelQuery = new QueryWrapper<NurseLevelEntity>().lambda()
                .eq(NurseLevelEntity::getLevelName, nurseLevelDTO.getLevelName())
                .eq(NurseLevelEntity::getDelFlag,0)
                .eq(NurseLevelEntity::getType,nurseLevelDTO.getType());
        int count = this.count(nurseLevelQuery);
        if (count > 0){
            return Rest.fail("护士等级名称已存在,请重新输入");
        }
        NurseLevelEntity nurseLevelEntity = new NurseLevelEntity();
        BeanUtils.copyProperties(nurseLevelDTO,nurseLevelEntity);
        nurseLevelEntity.setId(null);
        baseMapper.insert(nurseLevelEntity);
        return Rest.ok();
    }

    public Rest updateNurseLevel(NurseLevelDTO nurseLevelDTO) {
        LambdaQueryWrapper<NurseLevelEntity> nurseLevelQuery = new QueryWrapper<NurseLevelEntity>().lambda()
                .eq(NurseLevelEntity::getLevelName, nurseLevelDTO.getLevelName())
                .eq(NurseLevelEntity::getDelFlag,0)
                .ne(NurseLevelEntity::getId,nurseLevelDTO.getId())
                .eq(NurseLevelEntity::getType,nurseLevelDTO.getType());
        int count = this.count(nurseLevelQuery);
        if (count > 0){
            return Rest.fail("护士等级名称已存在,请重新输入");
        }
        NurseLevelEntity nurseLevelEntity = new NurseLevelEntity();
        BeanUtils.copyProperties(nurseLevelDTO,nurseLevelEntity);
        baseMapper.updateById(nurseLevelEntity);
        return Rest.ok();
    }

    public Rest delete(String id) {
        NurseLevelEntity nurseLevelEntity = new NurseLevelEntity();
        nurseLevelEntity.setId(id);
        nurseLevelEntity.setDelFlag(1);
        baseMapper.updateById(nurseLevelEntity);
        return Rest.ok();
    }

    public List<NurseLevelVo> listNurseLevel(NurseLevelDTO nurseLevelDTO) {
        List<NurseLevelVo> nurseLevelVos = baseMapper.listNurseLevel(nurseLevelDTO);
        return nurseLevelVos;
    }

    public List<NurseLevelWithNurseVo> listNurseLevelWithNurse(NurseLevelWithNurseDTO nurseLevelDTO) {
        List<NurseLevelWithNurseVo> nurseLevelWithNurseVos = baseMapper.listNurseLevelWithNurse(nurseLevelDTO);
        return nurseLevelWithNurseVos;
    }

    /**
     * 根据菜单列表 构建树型结构
     * @param levels 菜单列表
     * @return
     */
    public List<NurseLevelVo> toTree(List<NurseLevelVo> levels){
        //模拟根节点
        NurseLevelVo root = new NurseLevelVo();
        root.setId("0");
        root = toTree(root,levels);
        return root.getChildList();
    }

    /**
     * 根据菜单列表 构建树型结构
     * @param root 根节点
     * @param levels 菜单列表
     * @return
     */
    private NurseLevelVo toTree(NurseLevelVo root, List<NurseLevelVo> levels){
        //初始化根菜单的子菜单列表
        if (ObjectUtils.isEmpty(root.getChildList())){
            root.setChildList(new ArrayList<>());
        }
        //查找子菜单
        for (NurseLevelVo level : levels) {
            if (level.getPid().equals(root.getId())){
                NurseLevelVo treeNode = toTree(level, levels);
                root.getChildList().add(treeNode);
            }
        }
        return root;
    }
}
